package com.xt.partjobback.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xt.partjobback.constant.CommonConstant;
import com.xt.partjobback.constant.ResultDTO;
import com.xt.partjobback.dto.JobDto;
import com.xt.partjobback.entity.Classify;
import com.xt.partjobback.entity.Job;
import com.xt.partjobback.entity.Message;
import com.xt.partjobback.exception.OperateException;
import com.xt.partjobback.service.ClassIfyService;
import com.xt.partjobback.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author leotong
 */
@RestController
@RequestMapping(CommonConstant.API_URI_VERSION_V1+"/classify")
public class ClassifyController {

    @Autowired
    private ClassIfyService classIfyService;
    @Autowired
    private JobService jobService;
    @PostMapping("/update")
    public ResultDTO<Boolean> updateClassify(@RequestBody Classify classify){
        Classify name = classIfyService.getOne(new QueryWrapper<Classify>().eq("name", classify.getName()).ne("id",classify.getId()));
        if (name!=null){
            throw new OperateException("当前分类已经存在，请重新输入");
        }
        return ResultDTO.success(classIfyService.updateById(classify));
    }

    @GetMapping("/delete")
    public ResultDTO<Boolean> deleteClassify(Integer id){
        Job job=new Job();
        job.setCid(1);
        jobService.update(job,new QueryWrapper<Job>().eq("cid",id));
        return ResultDTO.success(classIfyService.removeById(id));
    }

    @PostMapping("/createClassify")
    public ResultDTO<Boolean> createClassify(@RequestBody Classify classify){
        Classify name = classIfyService.getOne(new QueryWrapper<Classify>().eq("name", classify.getName()));
        if (name!=null){
            throw new OperateException("当前分类已经存在，请重新输入");
        }
        classify.setCreateTime(new Date());
        return ResultDTO.success(classIfyService.save(classify));
    }

    @PostMapping("/getClassify")
    public ResultDTO<Page<Classify>> getClassify(@RequestBody Classify classify){
        return ResultDTO.success(classIfyService.getClassify(classify));
    }

    @PostMapping("/getAllClassify")
    public ResultDTO<List<Classify>> getAllClassify(){
        return ResultDTO.success(classIfyService.list());
    }
}
